<?php
/**
 * Clase Base FN para BaseMrpRecetaFN
 *
 * @package    
 * @subpackage BaseMrpRecetaFN
 * @author     Hans
 * @version    SVN:: BaseMrpRecetaFN.class.php,v 1.1 2012-03-07 02:02:08 hans.parra Exp $
 */
require($_SERVER['DOCUMENT_ROOT'].'/gourmet-express/trunk/src/class/mrp/recetas/BN/MrpRecetaBN.class.php');
class BaseMrpRecetaFN extends MrpRecetaBN
{
  private static $fieldNames = array (
    'RctRecetaId'=>'RCT_RECETAID', 'RctProductoId'=>'RCT_PRODUCTOID', 'RctDescripcion'=>'RCT_DESCRIPCION', 'RctObservaciones'=>'RCT_OBSERVACIONES', 'RctDetalles'=>'RCT_DETALLES','RctTipoProducto'=>'RCT_TIPOPRODUCTO', 'RctProductoCant'=>'RCT_PRODUCTOCANT', 'RctProductoUoM'=>'RCT_PRODUCTOUOM', 'RctPorcMerma'=>'RCT_PORCMERMA', 'SysInsUsr'=>'SYSINSUSR', 'SysInsAt'=>'SYSINSAT', 'SysLstUpdUsr'=>'SYSLSTUPDUSR', 'SysLstUpdAt'=>'SYSLSTUPDAT'  
  );

  public function select()
  {
    try
    {
    
      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();
    	
      $tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $this->numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;
    	
      $where = $this->buildConsult();
      //$orden = $this->buildOrderByField();
      $orden="";
      
      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();
       $sql = "SELECT rct_recetaId, rct_productoId, rct_descripcion, rct_detalles, rct_observaciones,rct_tipoProducto, rct_productoCant, rct_productoUoM, rct_porcMerma, SysInsUsr, SysInsAt, SysLstUpdUsr, SysLstUpdAt FROM mrp_receta WHERE 1 ".$where." ".$orden." LIMIT ".$limitInf.",".$tamPag;
      $result = mysql_db_query('main_db', $sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function selectNumRows()
  {
    try
    {
      $where = $this->buildConsult();

      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();
      $sql = "SELECT RCT_RECETAID, RCT_PRODUCTOID, RCT_DESCRIPCION, RCT_DETALLES,RCT_OBSERVACIONES, RCT_TIPOPRODUCTO, RCT_PRODUCTOCANT, RCT_PRODUCTOUOM, RCT_PORCMERMA,  SYSINSUSR, SYSINSAT, SYSLSTUPDUSR, SYSLSTUPDAT FROM mrp_receta WHERE 1 ".$where;
      $result = mysql_db_query('main_db', $sql);
      $num_rows = mysql_num_rows($result);
      
      return $num_rows;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  private function buildConsult()
  {
    try
    {
      $where = "";

      if($this->getRctRecetaId()){ $where .= " AND RCT_RECETAID = '".$this->getRctRecetaId()."'"; }
      if($this->getRctProductoId()){ $where .= " AND RCT_PRODUCTOID = '".$this->getRctProductoId()."'"; }
      if($this->getRctDescripcion()){ $where .= " AND RCT_DESCRIPCION like '%".$this->getRctDescripcion()."%'"; }
      if($this->getRctDetalles()){ $where .= " AND RCT_DETALLES = '".$this->getRctDetalles()."'"; }
      if($this->getRctObservaciones()){ $where .= " AND RCT_OBSERVACIONES = '".$this->getRctObservaciones()."'"; }
      if($this->getRctTipoProducto()=="PT"){
          $where .= " AND RCT_TIPOPRODUCTO = 'PT'"; 
         //if($this->getRctTipoProducto()){ $where .= " AND RCT_TIPOPRODUCTO = '".$this->getRctTipoProducto()."'"; }
      }else{
          $where .= " AND RCT_TIPOPRODUCTO <> 'PT'";
      }
      if($this->getRctProductoCant()){ $where .= " AND RCT_PRODUCTOCANT = '".$this->getRctProductoCant()."'"; }
      if($this->getRctProductoUoM()){ $where .= " AND RCT_PRODUCTOUOM = '".$this->getRctProductoUoM()."'"; }
      if($this->getRctPorcMerma()){ $where .= " AND RCT_PORCMERMA = '".$this->getRctPorcMerma()."'"; } 
      if($this->getSysInsUsr()){ $where .= " AND SYSINSUSR = '".$this->getSysInsUsr()."'"; }
      if($this->getSysInsAt()){ $where .= " AND SYSINSAT = '".$this->getSysInsAt()."'"; }
      if($this->getSysLstUpdUsr()){ $where .= " AND SYSLSTUPDUSR = '".$this->getSysLstUpdUsr()."'"; }
      if($this->getSysLstUpdAt()){ $where .= " AND SYSLSTUPDAT = '".$this->getSysLstUpdAt()."'"; }
      return $where;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildOrderByField()
  {
    try
    {
      $order_by = "";

      if($this->sortCampos || $this->sortCampos != ""){
        $a_valCampos = explode(";", $this->sortCampos);
        $a_valtype   = explode(";", $this->sortType);
        $order_by = " ORDER BY ";
        $a_order = array();
        foreach ($a_valCampos as $key => $val){
          $sTmpOrder = "";
          if(array_key_exists($val, self::$fieldNames)){
            $sTmpOrder .= self::$fieldNames[$val];
            switch ($a_valtype[$key]){
              case 1: $sTmpOrder .= " ASC"; break;
              case 2: $sTmpOrder .= " DESC"; break;
            }
            $a_order[] = $sTmpOrder;
          }
        }
        $order_by .= implode(", ", $a_order);
      }
      return $order_by;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* B�squeda del registro por la llave primaria */
  public function selectByPK()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con=$o_conexion->conectar();
      $sql = "SELECT rct_recetaId, rct_productoId, rct_descripcion, rct_detalles, rct_observaciones,rct_tipoProducto, rct_productoCant, rct_productoUoM, rct_porcMerma, SysInsUsr, SysInsAt, SysLstUpdUsr, SysLstUpdAt FROM mrp_receta WHERE 1 AND RCT_RECETAID='".$this->getRctRecetaId()."'; ";
      $result = mysql_db_query('main_db', $sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      

      return $o_Propel?$this->insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_BaseMrpRecetaFN = new MrpRecetaBN();
    $o_BaseMrpRecetaFN->setRctRecetaId($o_propel->rct_recetaId);
    $o_BaseMrpRecetaFN->setRctProductoId($o_propel->rct_productoId);
    $o_BaseMrpRecetaFN->setRctDescripcion($o_propel->rct_descripcion);
    $o_BaseMrpRecetaFN->setRctDetalles($o_propel->rct_detalles);
    $o_BaseMrpRecetaFN->setRctObservaciones($o_propel->rct_observaciones);    
    $o_BaseMrpRecetaFN->setRctTipoProducto($o_propel->rct_tipoProducto);
    $o_BaseMrpRecetaFN->setRctProductoCant($o_propel->rct_productoCant);
    $o_BaseMrpRecetaFN->setRctProductoUoM($o_propel->rct_productoUoM);
    $o_BaseMrpRecetaFN->setRctPorcMerma($o_propel->rct_porcMerma);
    $o_BaseMrpRecetaFN->setSysInsUsr($o_propel->SysInsUsr);
    $o_BaseMrpRecetaFN->setSysInsAt($o_propel->SysInsAt);
    $o_BaseMrpRecetaFN->setSysLstUpdUsr($o_propel->SysLstUpdUsr);
    $o_BaseMrpRecetaFN->setSysLstUpdAt($o_propel->SysLstUpdAt);
    return $o_BaseMrpRecetaFN;
  }
}